// 创建学生相关的小仓库
import { defineStore } from "pinia";
import type { StudentState } from "./types/types";
import type { studentAllResponseData, studentInfoData, studentListFormData, studentListResponseData, studentResponseData } from "@/api/student/type";
import { reqStudentAdd, reqStudentAll, reqStudentById, reqStudentDelete, reqStudentList, reqStudentUpdata } from "@/api/student";




let useStudentStore = defineStore("Student", {
    state: (): StudentState => {
        return {
           students: []
        }
    },
    actions: {
        // 获取所有学生
        async getStudentAll() {

            let result:  studentAllResponseData = await reqStudentAll()
            if (result.status == 0) {
                this. students = (result.data as  studentInfoData[])

                return 'ok'
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 分页获取所有学生
        async getStudentList(data:  studentListFormData) {

            let result:  studentListResponseData = await reqStudentList(data);
            if (result.status == 0) {
                this. students = (result.data?.data as  studentInfoData[])

                return result.data
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 添加学生
        async addStudent(data:  studentInfoData) {

            let result:  studentResponseData = await reqStudentAdd(data);
            if (result.status == 0) {
                return "ok"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 查找所有学生信息（通过id）
        async getStudentById(_id: string) {

            let result:  studentResponseData = await reqStudentById(_id);
            if (result.status == 0) {
                return result.data
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        //  修改学生信息
        async upDataStudent(data: studentInfoData) {

            let result:  studentResponseData = await reqStudentUpdata(data);
            if (result.status == 0) {
                return "ok"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 删除学生
        async deleteStudent( studentId: string) {

            let result:  studentResponseData = await reqStudentDelete( studentId);
            if (result.status == 0) {
                return "ok"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
    }
})
export default useStudentStore